package com.yonyou.pmclouds.processphoto.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.ProjectStatusCheckProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.processphoto.entity.ProcessPhotoVO;
import com.yonyou.pmclouds.processphoto.mapper.ProcessPhotoMapper;
import com.yonyou.pmclouds.processphoto.process.ProcessPhotoBeforeAddProcess;
import com.yonyou.pmclouds.processphoto.rmiitf.ProcessPhotoMaintain;
import com.yonyou.pmclouds.project.processor.ProjectBillUpdateForUpdate;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForDeleteProcessor;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForInsertProcessor;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import com.yonyou.pmclouds.workflow.service.rmiitf.IFlowApproveBuziService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass=ProcessPhotoMaintain.class)
public class ProcessPhotoMaintainImpl implements ProcessPhotoMaintain ,IFlowApproveBuziService {
    @Autowired
    private ProcessPhotoMapper mapper;
    @Autowired
    private PictureRemoteService pictureService;
    @Autowired
    private UpdateBillCountForInsertProcessor.UpdateBillCountForInsertProcessorFactory<ProcessPhotoVO> billCountFactory;
    @Autowired
    private ProjectStatusCheckProcessor<ProcessPhotoVO> projectStatusCheckProcessor;
    @Autowired
    private ProjectBillUpdateForUpdate.ProjectBillForUpdateProcessorFactory<ProcessPhotoVO> billForUpdateProcessorFactory;
    @Autowired
    private UpdateBillCountForDeleteProcessor.UpdateBillCountForDeleteProcessorFactory<ProcessPhotoVO> billCountDeleteFactory;

    @Override
    @PMApprove
    public ProcessPhotoVO save(ProcessPhotoVO vo) throws BusinessException {
        SingleInsertBpTemplate<ProcessPhotoVO> bp = new SingleInsertBpTemplate<ProcessPhotoVO>(mapper);
        bp.addBeforeProcessor(new ProcessPhotoBeforeAddProcess());
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<ProcessPhotoVO>());
        bp.addBeforeProcessor(projectStatusCheckProcessor);
        bp.addAfterProcessor(billCountFactory.getProcessor(BillTypeConst.PROCESSPHOTO_BILLTYPE));
        bp.insert(vo);
        // 保存图片
        pictureService.insertPictureForPub(vo, null);
        return vo;
    }


    @Override
    public SuperVO approveByPkBill(String pk_bill, byte billStatus) throws BusinessException {
        return approve(pk_bill,billStatus);
    }

    @Override
    public SuperVO rejectByPkBill(String pk_bill) throws BusinessException {
        return approve(pk_bill,BillStatusConst.COMMON_REJECT_STATUS);
    }

    @Override
    public SuperVO queryDetailForWorkFlow(String pk_bill) throws BusinessException {
        return mapper.getById(pk_bill);
    }

    @Override
    public String getBillType() {
        return BillTypeConst.PROCESSPHOTO_BILLTYPE;
    }

    public ProcessPhotoVO approve(String pkProcessphoto,byte billStatus) throws BusinessException {
        if(StringUtils.isEmpty(pkProcessphoto)){
            return null;
        }
        ProcessPhotoVO processPhotoVO = mapper.getById(pkProcessphoto);
        SingleUpdateBpTemplate<ProcessPhotoVO> bp = new SingleUpdateBpTemplate<>(mapper, new String[]{CommonFieldConst.BILLSTATUS});
        processPhotoVO.setBillstatus(billStatus);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.CONSTRUCTIONLOG_BILLTYPE));
        bp.update(processPhotoVO);

        return processPhotoVO;
    }

    @Override
    public SuperVO deleteBill(SuperVO businessVO) throws BusinessException {
        SingleDeleteBpTemplate<ProcessPhotoVO> deleteBpTemplate = new SingleDeleteBpTemplate<>(mapper);
        deleteBpTemplate.addAfterProcessor(billCountDeleteFactory.getProcessor(BillTypeConst.PROCESSPHOTO_BILLTYPE));
        return deleteBpTemplate.delete((ProcessPhotoVO) businessVO);
    }

    @Override
    public Object getMapper() {
        return mapper;
    }
}
